﻿using System;
using System.Collections.Generic;
using System.Text;
using AMS.DataAccess.Common;
using AMS.DataAccess.Sql;
using AMS.DataAccess.Trademark.BO;
using AMS.DataAccess.Trademark.Interfaces;
using AMS.DataAccess.Trademark.Filters;
using AMS.DataAccess.Trademark.BO.Enums;
using System.Data;

namespace AMS.DataAccess.Trademark
{
    public class TrademarkManager : ITrademarkManager
    {
        public void AddTrademark(CallContext context, TrademarkInfo tm)
        {
            SqlInsert insert = new SqlInsert("TT_Trademark");
            insert.AddBusinessObject(tm);
            insert.Add("SELECT @@IDENTITY");

            AMS.DataAccess.SqlDebugger.WriteLine(insert);

            var ds = SqlDBHelper.Query(insert.ToString());
            tm.AID = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
        }

        public bool UpdateTrademark(CallContext context, TrademarkInfo trademark)
        {
            SqlUpdate update = new SqlUpdate("TT_Trademark");
            update.AddBusinessObject(trademark);
            update.AddCondition(new SqlCondition("AID", trademark.AID));

            AMS.DataAccess.SqlDebugger.WriteLine(update);

            return 0 < SqlDBHelper.ExecuteSql(update.ToString());
        }

        public TrademarkInfo GetTrademark(CallContext context, int trademarkId)
        {
            SqlBuilder sql = new SqlBuilder();
            sql.Add("SELECT *");
            sql.Add("FROM TT_Trademark");
            sql.Add("WHERE");
            sql.Add("    ", new SqlCondition("AID", trademarkId));

            AMS.DataAccess.SqlDebugger.WriteLine(sql);

            var ds = SqlDBHelper.Query(sql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                var tm = BusinessObjectMapping.DataRowToBO<TrademarkInfo>(ds.Tables[0].Rows[0]);
                return tm;
            }
            else
            {
                return null;
            }
        }

        public TrademarkInfo GetTrademarkByCase(CallContext context, int caseId)
        {
            SqlBuilder sql = new SqlBuilder();
            sql.Add("SELECT t.*");
            sql.Add("FROM TT_Trademark t");
            sql.Add("INNER JOIN TT_CaseBasicInfo c ON t.AID = c.TrademarkID");
            sql.Add("WHERE");
            sql.Add("    ", new SqlCondition("c.AID", caseId));

            AMS.DataAccess.SqlDebugger.WriteLine(sql);

            var ds = SqlDBHelper.Query(sql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                var tm = BusinessObjectMapping.DataRowToBO<TrademarkInfo>(ds.Tables[0].Rows[0]);
                return tm;
            }
            else
            {
                return null;
            }
        }

        public List<TrademarkInfo> GetTrademarkList(CallContext context, TrademarkFilter filter)
        {
            SqlBuilder sql = new SqlBuilder();
            sql.Add("SELECT t.AID");
            sql.Add("      ,t.CustomerID");
            sql.Add("      ,t.Name");
            sql.Add("      ,t.NameE");
            sql.Add("      ,t.RegisterDate");
            sql.Add("      ,t.RegisterNumber");
            sql.Add("      ,t.RegisterUser");
            sql.Add("      ,t.RegisterAddress");
            sql.Add("      ,t.RegisterZipcode");
            sql.Add("      ,t.ApplyDate");
            sql.Add("      ,t.ApplyNumber");
            sql.Add("      ,t.CreateDateTime");
            sql.Add("      ,c.Name AS CustomerName");
            sql.Add("FROM TT_Trademark t");
            sql.Add("INNER JOIN TC_Customer c ON c.AID = t.CustomerID");
            sql.Add("WHERE 1=1");
            if (filter != null)
            {
                if (filter.TrademarkName != null)
                {
                    sql.Add("    AND Name", filter.TrademarkName.ToString());
                }
                if (filter.TrademarkNameE != null)
                {
                    sql.Add("    AND NameE", filter.TrademarkNameE.ToString());
                }
                if (filter.TrademarkType != null)
                {
                    sql.Add("    AND TrademarkType", filter.TrademarkType.ToString());
                }
                if (filter.CustomerIDs.Count > 0)
                {
                    sql.Add("    AND ", new SqlConditionList("CustomerID", filter.CustomerIDs));
                }
            }

            AMS.DataAccess.SqlDebugger.WriteLine(sql);

            var ds = SqlDBHelper.Query(sql.ToString());
            var list = BusinessObjectMapping.DataTableToBOList<TrademarkInfo>(ds.Tables[0]);
            return list;
        }

        public DataTable GetTrademarkStatusList(CallContext context)
        {
            string sql = "SELECT StatusID, StatusName FROM TTD_TrademarkStatus";
            var ds = SqlDBHelper.Query(sql);
            return ds.Tables[0];
        }
    }
}
